package com.lollipop.rabbitmqspringboot.controller;

import com.lollipop.rabbitmqspringboot.config.ConfirmConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author: Lollipop
 * @Date: 2021-10-18 20:38
 * @Description: 发布确认模式-消息生产者
 */
@RestController
@Slf4j
@RequestMapping("confirm")
public class ProducerController {

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 消息生产者-发送消息
     *
     * @param msg
     */
    @GetMapping("sendMessage/{msg}")
    public void sendMessage(@PathVariable String msg) {
        CorrelationData correlationData = new CorrelationData("1");
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME, ConfirmConfig.CONFIRM_ROUTING_KEY, msg, correlationData);
        log.info("发送的消息：{}-{}", msg, "key1");
        CorrelationData correlationData2 = new CorrelationData("2");
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME, ConfirmConfig.CONFIRM_ROUTING_KEY + "1", msg, correlationData2);
        log.info("发送的消息：{}-{}", msg, "key2");
    }
}
